package com.leyou.item.controller;

import com.leyou.common.domain.PageResult;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RefreshScope
@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @GetMapping("/page")
    public ResponseEntity<PageResult<BrandDTO>> brandPageQuery(@RequestParam(value = "key", required = false) String key,
                                                               @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                               @RequestParam(value = "rows", defaultValue = "5") Integer rows,
                                                               @RequestParam("sortBy") String sortBy,
                                                               @RequestParam(value = "desc", defaultValue = "false") Boolean desc){
        PageResult<BrandDTO> pageResult = brandService.brandPageQuery(key, page, rows, sortBy, desc);
        return ResponseEntity.ok(pageResult);
    }

    @PostMapping
    public ResponseEntity<Void> saveBrand(BrandDTO brandDTO, @RequestParam("cids")List<Long> cids){
        brandService.saveBrand(brandDTO, cids);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @GetMapping("/{id}")
    public ResponseEntity<BrandDTO> findBrandById(@PathVariable("id") Long id){
        BrandDTO brandDTO = brandService.findBrandById(id);
        return ResponseEntity.ok(brandDTO);
    }

    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> findBrandsByCid(@RequestParam("id") Long id){
        List<BrandDTO> list = brandService.findBrandsByCid(id);
        return ResponseEntity.ok(list);
    }


}
